package graphicsExtended.animation;

import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.event.WindowStateListener;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

/**
 *
 * Fully Tested: No
 * Fully Documented: No
 * Created: 29-May-2012
 * Last Updated: 29-May-2012
 *
 * @author Shimu Wu
 */
public class AnimationFrame extends JFrame implements Runnable {

    private static final long serialVersionUID = 1L;

    private InteractiveAnimationPanel panel;

    private final JTextArea textArea;

    private final JScrollPane scrollPane;

    public AnimationFrame(int fps) {
        this.textArea = new JTextArea();

        /*
        this.textArea.setEditable(false);
        int textAreaHeight = (int) (panel.getHeight() * 0.20);
        this.textArea.setPreferredSize(new Dimension(
        panel.getWidth(), textAreaHeight));
        
        this.setPreferredSize(new Dimension(panel.getWidth(), 
        panel.getHeight() + textAreaHeight));
         * */
        this.scrollPane = new JScrollPane(this.textArea);

        /*
        DefaultCaret caret = (DefaultCaret) textArea.getCaret();
        caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
        this.scrollPane.setVerticalScrollBarPolicy(
        JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        
        this.getContentPane().setLayout(new BoxLayout(getContentPane(),
        BoxLayout.Y_AXIS));
        
        this.add(scrollPane);
         * 
         */
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        this.addWindowListener(new WindowAdapter(){
            @Override
            public void windowClosing(WindowEvent e) {
                if (JOptionPane.showConfirmDialog(AnimationFrame.this, 
                        "Are you sure you want to quit?", "Exit", 
                        JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
                    System.out.println("Thank you for playing");
                    AnimationFrame.this.dispose();
                    System.exit(0);
                }
            }
        });
        //this.redirectSystemStreams();
        this.pack();
    }

    // ============== COPIED FROM INTERNET ==============
    private void updateTextArea(final String text) {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                textArea.setText(text);
                textArea.setCaretPosition(textArea.getDocument().getLength());
            }
        });
    }

    private void redirectSystemStreams() {
        OutputStream out = new OutputStream() {

            @Override
            public void write(int b) throws IOException {
                updateTextArea(String.valueOf((char) b));
            }

            @Override
            public void write(byte[] b, int off, int len) throws IOException {
                updateTextArea(new String(b, off, len));
            }

            @Override
            public void write(byte[] b) throws IOException {
                write(b, 0, b.length);
            }
        };

        //System.setOut(new PrintStream(out, true));
        System.setErr(new PrintStream(out, true));
    }
    // ============== END FROM INTERNET ==============

    public void addMouseListener(MouseListener... listeners) {
        for (MouseListener m : listeners) {
            this.addMouseListener(m);
        }
    }

    public final void setPanel(InteractiveAnimationPanel panel) {
        this.panel = panel;
        this.setSize(panel.getSize());
        this.add(panel);
        //this.add(textArea);
        this.addMouseListener(panel);
        this.addMouseMotionListener(panel);
    }

    @Override
    public void run() {
        if (this.panel == null) {
            System.err.println("InteractiveAnimationPanel not set to this"
                    + " AnimationFrame2 yet!");
            System.exit(1);
        } else {
            this.panel.run();
            this.setVisible(true);
        }
    }
}
